using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Net.Mail;



public class MailHelper
{

    public static void SendMailMessage(string to, string cc, string bcc, MailMerge msg)
    {
        SendMailMessage( to, bcc, cc, msg.Subject,msg.Body );
    }

    /// <summary>
    /// Sends an mail message
    /// </summary>
    /// <param name="from">Sender address</param>
    /// <param name="to">Recepient address</param>
    /// <param name="bcc">Bcc recepient</param>
    /// <param name="cc">Cc recepient</param>
    /// <param name="subject">Sub ject of mail message</param>
    /// <param name="body">Body of mail message</param>
    public static void SendMailMessage(string to, string cc, string bcc, string subject, string body)
    {
        try
        {
            // Instantiate a new instance of MailMessage
            MailMessage mMailMessage = new MailMessage();

            // Set the sender address of the mail message
            mMailMessage.From = new MailAddress(System.Configuration.ConfigurationManager.AppSettings.Get("SMTPFrom"), System.Configuration.ConfigurationManager.AppSettings.Get("SMTPDisplayName"));
            // Set the recepient address of the mail message
            mMailMessage.To.Add(new MailAddress( to==""?System.Configuration.ConfigurationManager.AppSettings.Get("SMTPTo"):to));

            // Check if the bcc value is null or an empty string
            if ((bcc != null) && (bcc != string.Empty))
            {
                // Set the Bcc address of the mail message
                mMailMessage.Bcc.Add(new MailAddress(bcc));
            }      // Check if the cc value is null or an empty value
            if ((cc != null) && (cc != string.Empty))
            {
                // Set the CC address of the mail message
                mMailMessage.CC.Add(new MailAddress(cc));
            }       // Set the subject of the mail message
            mMailMessage.Subject = subject;
            // Set the body of the mail message
            mMailMessage.Body = body;

            // Set the format of the mail message body as HTML
            mMailMessage.IsBodyHtml = true;
            // Set the priority of the mail mexessage to normal
            mMailMessage.Priority = MailPriority.Normal;

            // Instantiate a new instance of SmtpClient
            SmtpClient mSmtpClient = new SmtpClient();
            mSmtpClient.Host = System.Configuration.ConfigurationManager.AppSettings.Get("SMTPServer");
            mSmtpClient.Port = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings.Get("SMTPPort"));
            mSmtpClient.UseDefaultCredentials = false;

            mSmtpClient.Credentials = new System.Net.NetworkCredential
                 (System.Configuration.ConfigurationManager.AppSettings.Get("SMTPUser"),
                 System.Configuration.ConfigurationManager.AppSettings.Get("SMTPPassword"));
            //Or your Smtp Email ID and Password
            mSmtpClient.EnableSsl = Convert.ToBoolean(System.Configuration.ConfigurationManager.AppSettings.Get("SMTPUseSsl"));

            // Send the mail message
            //check if mails should be sent
            if (Convert.ToBoolean(System.Configuration.ConfigurationManager.AppSettings.Get("EnableMails")))
                mSmtpClient.Send(mMailMessage);
        }
        catch (Exception ex)
        {
            //TODO: Store mail in database for action later
        }

    
    }
}
